<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2019/11/12
 * Time: 17:10
 */

namespace app\common\service;


class IpsRule
{
    /**
     * @name 列表
     * @param $host_id
     * @param $params
     * @param $limit
     * @return array
     * @author wx
     * @date 2019/11/13 10:43
     */
    function list($host_id, $params, $limit)
    {
        $db = db('ips_rule')->alias('ir')
            ->join('ips_type it', 'ir.type = it.id')
            ->where('host_id', $host_id)->where('fid',0);


        !isset($params['name']) || empty($params['name']) ? : $db->whereLike('name', '%'. $params['name'] . '%' );
        !isset($params['description']) || empty($params['description']) ? : $db->whereLike('description', '%'. $params['description'] . '%' );
        !isset($params['content']) || empty($params['content']) ? : $db->whereLike('content', '%'. $params['content'] . '%' );
        !isset($params['type']) || !is_numeric($params['type']) ? : $db->where('it.id', $params['type']);
        !isset($params['handle']) || !is_numeric($params['handle']) ? : $db->where('handle', $params['handle']);
        !isset($params['status']) || !is_numeric($params['status']) ? : $db->where('status', $params['status']);

        $field = 'ir.id, ir.name, ir.description, ir.type as type_id, it.show_name as type, ir.content, ir.handle, ir.status';

        $return = $db->field($field)->paginate($limit)->toArray();


        return $return;
    }

    /**
     * @name 修改
     * @param $condition
     * @param $updateData
     * @return int|string
     * @author wx
     * @date 2019/11/13 10:43
     */
    function modify($condition, $updateData)
    {
        return db('ips_rule')->where($condition)->update($updateData);
    }

    /**
     * @name 删除
     * @param $condition
     * @return int
     * @author wx
     * @date 2019/11/13 10:49
     */
    function del($condition)
    {
        return db('ips_rule')->where($condition)->delete();
    }

    /**
     * @name 清空数据表
     * @author wx
     * @date 2019/11/13 10:55
     */
    function clear($host_id)
    {
        $databaseConf = config('database.');
        $prefix = $databaseConf['prefix'];
        $table = 'ips_rule';
        db()->query("TRUNCATE TABLE ".$prefix.$table);
    }

    /**
     * @name 添加
     * @param $addData
     * @return int|string
     * @author wx
     * @date 2019/11/13 16:50
     */
    function add($addData)
    {
        return db('ips_rule')->insertGetId($addData);
    }
}